package com.web4code.codegen.generator;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.velocity.VelocityContext;

import com.web4code.codegen.domain.GenProject;
import com.web4code.codegen.utils.DateUtils;
import com.web4code.codegen.utils.StringUtils;

public class VelocityProjectUtils {

    /** 项目空间路径 */
    private static final String PROJECT_PATH = "main/java";

    /**
     * 设置模板变量信息
     *
     * @return 模板列表
     */
    public static VelocityContext prepareContext(GenProject genProject)
    {
        String moduleName = genProject.getModuleName();
        String businessName = genProject.getBusinessName();
        String packageName = genProject.getPackageName();

        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put("moduleName", genProject.getModuleName());
        velocityContext.put("BusinessName", StringUtils.capitalize(genProject.getBusinessName()));
        velocityContext.put("businessName", genProject.getBusinessName());
        velocityContext.put("basePackage", VelocityUtils.getPackagePrefix(packageName));
        velocityContext.put("packageName", packageName);
        velocityContext.put("author", genProject.getFunctionAuthor());
        velocityContext.put("datetime", DateUtils.getDate());
        velocityContext.put("project", genProject);
        // velocityContext.put("dicts", getDicts(genTable));
        return velocityContext;
    }

    /**
     * 获取模板信息
     *
     * @return 模板列表
     */
    public static List<String> getTemplateList()
    {
        List<String> templates = new ArrayList<String>();
        templates.add("archetype-resources" + File.separator + "pom.xml");
        return templates;
    }

    /**
     * 获取文件名
     */
    public static String getFileName(String template, GenProject genProject)
    {
        // 文件名称
        String fileName = "";
        // 包路径
        String packageName = genProject.getPackageName();
        // 模块名
        String moduleName = genProject.getModuleName();
        // 大写类名
        String className = "TestClass";// genProject.getClassName();
        // 业务名称
        String businessName = genProject.getBusinessName();

        String javaPath = PROJECT_PATH + "/" + StringUtils.replace(packageName, ".", "/");

        if (template.contains("pom.xml"))
        {
            fileName = template;
        }
        else if (template.contains("domain.java.vm"))
        {
            fileName = StringUtils.format("{}/domain/{}.java", javaPath, className);
        }
        else if (template.contains("mapper.java.vm"))
        {
            fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className);
        }
        else if (template.contains("service.java.vm"))
        {
            fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className);
        }
        else if (template.contains("serviceImpl.java.vm"))
        {
            fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, className);
        }
        else if (template.contains("controller.java.vm"))
        {
            fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, className);
        }
        return fileName;
    }

}
